package com.cd826dong.clouddemo.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Component
public class UserContextFilter implements Filter {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httprequest1 = (HttpServletRequest)request;

        String userId = ((HttpServletRequest) request).getHeader(UserContext.USER_ID);
        String authToken = ((HttpServletRequest) request).getHeader(UserContext.AUTH_TOKEN);
        String shopId = ((HttpServletRequest) request).getHeader(UserContext.SHOP_ID);
        UserContext.setUserId(userId);
        UserContext.setAuthToken(authToken);
        UserContext.setShopId(shopId);
        this.logger.debug("Current shop.id = {}", UserContext.getShopId());
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }
}
